<?php
class Descripcion extends Doctrine_Record {
    public function setTableDefinition() {
        $this->setTableName('descripcion');
        $this->hasColumn('value', 'string', 255);
        $this->hasColumn('language', 'string', 2);
    }
    
    public static function autocomplete ($q, $language) {
        $q = Doctrine_Query::create()
            ->select('DISTINCT d.value')
            ->from('Descripcion d')
            ->where('d.language = ?', $language)
            ->andWhere('d.value like ?', $q.'%')
            ->limit(5)
            ->offset(0)
            ->orderBy('d.value');
        return $q->execute();
    }
    
    public static function existe ($value, $language) {
        $q = Doctrine_Query::create()
            ->select('COUNT(d.id) as total')
            ->from('Descripcion d')
            ->where('d.value = ? ', $value)
            ->andWhere('d.language = ? ', $language);
        $total = $q->execute();
        return ($total->count())?$total[0]->total:0;
    }
}
?>